package org.eduapp.trains.utils;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

/**
 * 
 * @author jeccy
 *
 */
public class UtilsAppCore {
	
	private static final String patternTime = "((\\d+)[^\\d]+)?(\\d+)[^\\d]+";
	private static final String patternStopTime = "(\\d+):(\\d+)";
	
	/**
	 * get minute time by the given train time 
	 * @param trainTime
	 * @return
	 */
	public static Integer getTimeMinByTrainTime (String trainTime) {
		if (UtilsString.isNotEmpty(trainTime)) {
			try {
				PatternCompiler patternCompiler = new Perl5Compiler();
				PatternMatcher patternMatcher = new Perl5Matcher();
				Pattern ptn = patternCompiler.compile(patternStopTime);
				if (patternMatcher.contains(trainTime, ptn)) {
					MatchResult result = patternMatcher.getMatch();
					int timeHour = result.group(1) != null ? Integer.parseInt(result.group(1)) : 0;
					int timeMin = result.group(2) != null ? Integer.parseInt(result.group(2)) : 0;
					return timeHour * 60 + timeMin;
				}
			} catch (MalformedPatternException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * get time min by given two sub time
	 * 
	 * @param trainTimeSrc
	 * @param trainTimeDst
	 * @return
	 */
	public static Integer getTimeMinByTrainTimeSub (String trainTimeSrc, String trainTimeDst) {
		if (UtilsString.isNotEmpty(trainTimeSrc) && UtilsString.isNotEmpty(trainTimeDst)) {
			Integer timeMinSrc = getTimeMinByTrainTime(trainTimeSrc);
			Integer timeMinDst = getTimeMinByTrainTime(trainTimeDst);
			return timeMinDst != null && timeMinSrc != null ? (timeMinDst - timeMinSrc) : 0;
		}
		return null;
	}
	
	/**
	 * get minute time by the given train runtime
	 * @param trainRuntime
	 * @return
	 */
	public static Integer getTimeMinByTrainRuntime (String trainRuntime) {
		if (UtilsString.isNotEmpty(trainRuntime)) {
			try {
				PatternCompiler patternCompiler = new Perl5Compiler();
				PatternMatcher patternMatcher = new Perl5Matcher();
				Pattern ptn = patternCompiler.compile(patternTime);
				if (patternMatcher.contains(trainRuntime, ptn)) {
					MatchResult result = patternMatcher.getMatch();
					int timeHour = result.group(2) != null ? Integer.parseInt(result.group(2)) : 0;
					int timeMin = result.group(3) != null ? Integer.parseInt(result.group(3)) : 0;
					return timeHour * 60 + timeMin;
				}
			} catch (MalformedPatternException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * get time string by given time minute
	 * @param timeMin
	 * @return
	 */
	public static String getTimeStrByMin (Integer timeMin) {
		if (timeMin != null) {
			int strHour = timeMin / 60;
			int strMin = timeMin - strHour * 60;
			return (strHour > 0 ? (strHour + "小时") : "") + strMin + "分"; 
		}
		return null;
	}
	
	/**
	 * get time string by given two sub time
	 * @param runTimeSrc
	 * @param runTimeDst
	 * @return
	 */
	public static String getTimeStrBySub (String runTimeSrc, String runTimeDst) { 
		return getTimeStrBySub(runTimeSrc, runTimeDst, 0);
	}
	
	/**
	 * get time string by given two sub time
	 * @param runTimeSrc
	 * @param runTimeDst
	 * @return
	 */
	public static String getTimeStrBySub (String runTimeSrc, String runTimeDst, int offset) {
		if (UtilsString.isNotEmpty(runTimeSrc) && UtilsString.isNotEmpty(runTimeDst)) {
			int timeMinSrc = getTimeMinByTrainRuntime(runTimeSrc);
			int timeMinDst = getTimeMinByTrainRuntime(runTimeDst);
			return getTimeStrByMin(timeMinDst - timeMinSrc - offset);
		}
		return null;
	}
	
	public static void main (String[] args) {
		System.out.println(getTimeStrByMin(UtilsAppCore.getTimeMinByTrainRuntime("2时2分")));
	}
	
}
